<?php

namespace app\stores\logic\order;


use app\beautiful\model\LevelModel;
use app\stores\model\order\LevelOrderModel;

class OrderPayMemberLogic{

    use OrderTrait;


    public function createOrder($params)

    {
        //判断用户

        empty($params['member_id']) && $this->error('请先登录后再操作');

        $this->checkMember($params['member_id'], false);

        //判断

        if($params['level_id']<1) return $this->error('参数有误');

        $model = new LevelModel();

        if(null === $level = $model->getById(['shop_id'=>$this->shopId,'level_id'=>$params['level_id'],'is_delete'=>0])) return $this->error('等级不存在');

        $member_level = getMemberLevel($params['member_id'],$this->shopId);

        if($member_level>0) return $this->error('你已经是会员');

        //判断支付方式
        $return = checkPayWay($params,$level['price'],$this->member->member_id,$this->shopId);

        if($return['error']){
            $this->error($return['error']);
        }else{
            $mix_pay_money = $return['mix_pay_money'];
        }

        //保存订单
        $order = new LevelOrderModel();

        $order_data=[
            'shop_id'=>$this->shopId,
            'member_id'=>$params['member_id'],
            'level_id'=>$params['level_id'],
            'pay_money'=>$level['price'],
            'mix_pay_type'=>(isset($mix_pay_money) && $mix_pay_money)?implode(',',array_keys($mix_pay_money)):null,
            'platform_money'=>isset($mix_pay_money['platform_money'])?$mix_pay_money['platform_money']:0,
            'money'=>isset($mix_pay_money['money'])?$mix_pay_money['money']:0,
            'weixin'=>isset($mix_pay_money['weixin'])?$mix_pay_money['weixin']:0,
            'edit_time'=>date('Y-m-d H:i:d'),
            'add_time'=>date('Y-m-d H:i:d'),
            'add_ip'=>request()->ip()
        ];
        $order_id = $order->insertGetId($order_data);

        LevelOrderModel::where(['order_id'=>$order_id])->save(['order_sn'=>createOrderSn($order_id,6)]);

        return $order_id;

    }




}